package com.army.study.comicqixi.utils;

import com.army.study.comicqixi.ResourceTable;
import com.army.study.comicqixi.database.Form;
import ohos.agp.components.ComponentProvider;
import ohos.app.Context;
import ohos.utils.zson.ZSONObject;

public class ComponentProviderUtils {
    public static final int DEFAULT_DIMENSION_2X2 = 2;
    public static final int DIMENSION_1X2 = 1;
    public static final int DIMENSION_2X4 = 3;
    public static final int DIMENSION_4X4 = 4;

    public static ComponentProvider getComponentProvider(Form form, Context context, ZSONObject zsonObject) {
        // 默认为2X2规格布置
        int layoutId = ResourceTable.Layout_form_immersive_pattern_widget_2_2;
        if (form.getDimension() == DIMENSION_2X4) {
            // 设置规格布置为2X4
            layoutId = ResourceTable.Layout_form_immersive_pattern_widget_2_4;
        }else if (form.getDimension() == DIMENSION_4X4) {
            // 设置规格布置为4X4
            layoutId = ResourceTable.Layout_form_immersive_pattern_widget_4_4;
        }else if (form.getDimension() == DIMENSION_1X2) {
            // 设置规格布置为1X2
            layoutId = ResourceTable.Layout_form_immersive_pattern_widget_1_2;
        }
        ComponentProvider componentProvider = new ComponentProvider(layoutId, context);
        // 更新卡片组件内容
        setComponentProviderValue(form, componentProvider, zsonObject);
        return componentProvider;
    }

    private static void setComponentProviderValue(Form form, ComponentProvider componentProvider, ZSONObject zsonObject) {
        // zsonObject为要更新的文本对象
        // 更新参数里显示四个字祝贺字的文本组件
        componentProvider.setText(ResourceTable.Id_fourCard, zsonObject.getString("fourText"));
        if (form.getDimension() != DIMENSION_1X2) {
            // 更新参数里显示八个字祝贺字的文本组件
            componentProvider.setText(ResourceTable.Id_eightCard, zsonObject.getString("eightText"));
        }
    }

}
